window.onload = function() {
        changeradio('ox');
    }
    //radio按钮和对应的span索引关系，如果增加新的词库，也要对应增加新的span和radio
var radiolist = ["ox", "cake", "new", "easy", "oxnew", "recent"];
var spanlist = {
    "ox": "spanox",
    "cake": "spancake",
    "new": "spannew",
    "easy": "spaneasy",
    "oxnew": "spanoxnew",
    "recent": "spanrecent"
};

function btnok() {
    //todo:遍历radiolist数组，确定哪个是checked，然后抽取对应wordlist里的单词。
    for (var i in radiolist) {
        if (document.getElementById(radiolist[i]).checked == true) {
            var checkedradio = radiolist[i];
        }
    }
    //此集合是radio按钮对应词库的关系，通过radio的名字索引对应的词库，如果增加新的词库，该索引也要更新
    var chooselist = {
        "ox": "wordlistox",
        "cake": "wordlistcake",
        "easy": "wordlisteasy",
        "new": "wordlisttemp",
        "oxnew": "wordlistoxnew",
        "recent": "wordlistrecent"
    };
    //抽取数组名称eval(chooselist[checkedradio])[0]
    var num = eval(chooselist[checkedradio]).length;
    var randomnum = Math.floor(Math.random() * num)
    document.getElementById('worden').value = eval(chooselist[checkedradio])[randomnum];
    fanyi(eval(chooselist[checkedradio])[randomnum]);
    var wordchoose = eval(chooselist[checkedradio])[randomnum];
    //var iframestyle = 'style="background-color: lightpink"';
    document.getElementById('divaudio').innerHTML = '<iframe id="frameaudio" src="http://dict.youdao.com/dictvoice?audio=' + wordchoose + '"></iframe>';
    document.getElementById('divfanyi').innerHTML = '';
}

//改变radio按钮的选中状态和背景色
function changeradio(s) {

    for (var i in radiolist) {
        document.getElementById(radiolist[i]).checked = false;
        document.getElementById(spanlist[radiolist[i]]).style.backgroundColor = "white";
    }
    document.getElementById(s).checked = true;
    document.getElementById(spanlist[s]).style.backgroundColor = "lightpink";

}
//隐藏、显示英语单词的div
function hidediven() {

    if (document.getElementById("btnhideen").value == "隐藏英语") {
        document.getElementById("btnhideen").value = "显示英语";
        document.getElementById("diven").style.opacity = 0;
    } else {
        document.getElementById("btnhideen").value = "隐藏英语";
        document.getElementById("diven").style.opacity = 1;
    }
}
// 隐藏、显示汉语翻译的div
function hidedivzh() {

    if (document.getElementById("btnhidezh").value == "隐藏汉语") {
        document.getElementById("btnhidezh").value = "显示汉语";
        document.getElementById("divzh").style.opacity = 0;
    } else {
        document.getElementById("btnhidezh").value = "隐藏汉语";
        document.getElementById("divzh").style.opacity = 1;
    }
}

//调用百度翻译，异步的问题没有解决，所以没法用函数返回翻译结果，只能在内部直接把翻译结果赋值给文本框
function fanyi(intxt) {
    for (var m in wordlistentozh) {
        if (m.toUpperCase() == intxt.toUpperCase()) {
            document.getElementById("wordzh").value = wordlistentozh[m];
            return true;
        }

    }
    var appid = '20180718000186694';
    var key = '6hlRV5ytiy0S_8QKQRMJ';
    var salt = 1435660288; //(new Date).getTime();
    var query = 'apple';
    // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
    var from = 'en';
    var to = 'zh';
    var str1 = appid + query + salt + key;
    var sign = MD5(str1);
    //var intxt = document.getElementById("txt01").value;
    var inmd5 = appid + intxt + salt + key;
    var outmd5 = MD5(inmd5);
    var httpadrs = "https://api.fanyi.baidu.com/api/trans/vip/translate?q=" + intxt + "&from=en&to=zh&appid=20180718000186694&salt=1435660288&sign=" + outmd5;
    var outtxt = 'false';
    $.ajax({
        url: httpadrs,
        type: 'GET',
        dataType: 'JSONP',
        async: false,
        success: function(data) {
            //提取json中的汉语翻译部分
            outtxt = data.trans_result[0].dst;
            document.getElementById("wordzh").value = outtxt;
        }
    });
}

function showaudio() {

}

function showdetail() {
    document.getElementById('divfanyi').innerHTML = '<iframe id="framefanyi" src="https://m.youdao.com/dict?le=eng&q=' + document.getElementById('worden').value + '"></iframe>';
}